<{include file="public_html/header.html"}>
<{include file="public_html/left_menu.html"}>
<!--公共部分-->
    
        <link href="/static/school/css/layout.css" type="text/css" rel="stylesheet" />
    <script src="/static/public/jquery-1.11.1.min.js" type="text/javascript"></script>
    <!--私有部分-->
    <link href="/static/school/css/frame.style.css" type="text/css" rel="stylesheet" />

    <link href="/static/school/css/default.style.css" type="text/css" rel="stylesheet" />
    <script src="/static/public/layer/src/layui.js"></script>
    <script src="/static/public/layer/load_layui.js" type="text/javascript"></script>
    <script src="/static/school/js/default.common.js" type="text/javascript"></script>
	<script type="text/javascript">
		var dire = 'user';
		var thumb = 'big_thumb,middle_thumb,small_thumb';
		var fileNumLimit = 6;
	</script>
				<!--star内容区域-->
				<div class="mainScroll">
					<div class="mainContainer line-all">

						<ul class="ui-nav-tab wList33">
							<li class="js-step-1"><a href="javascript:;">1.编辑课程基本信息</a></li>
							<li class="js-step-2 active"><a href="javascript:;">2.编辑课程目录</a></li>
						</ul>
						<div class="tabMainCont">

							<!--star 编辑课程目录-->
							<div class="tabItem step-02">
								<!--star-->
								<input type="hidden" id="course_id" value="<{$course.id|default:""}>">
								<div class="fix line-all p30 fs14">
									<div class="edit_course_list">
										<{if isset($chapter)&& !empty($chapter)}>
										<{foreach from = $chapter item = item}>
										<dl>
											<dt>
				                                <div class="editTit"><span><{$item.name}></span></div>
				                                <div class="editBtn tar">
				                                    <a href="javascript:;" class="save" vid="" describe="" v_name="" chapter_id="<{$item.id}>"><span class="ebj">✎</span>编辑</a>
				                                	<a href="javascript:;" class="del" chapter_id="<{$item.id}>" onclick="jsRemove(this,'dl');"><span>&times;</span>删除</a>
				                                </div>
				                            </dt>
				                            <{foreach from=$item.child item = sitem}>
											<dd>
												<div class="editTit"><span><{$sitem.name}></span></div>
												<div class="editBtn tar">
													<a href="javascript:;" class="save" vid="<{$sitem.vid}>" v_name="<{$sitem.v_name}>" describe="<{$sitem.describe}>" chapter_id="<{$sitem.id}>" ><span class="ebj">✎</span>编辑</a>
													<a href="javascript:;" class="del" chapter_id="<{$sitem.id}>" onclick="jsRemove(this,'dd');"><span>&times;</span>删除</a>
												</div>
											</dd>
											<{/foreach}>
											<dd class="bookAdd">
												<div class="editTit"><a href="javascript:;" onclick="jsBook_sub(this);"><span>+</span>添加一讲</a></div>
											</dd>
										</dl>
										<{/foreach}>
										<{else}>
										<dl>
											<dt>
				                                <div class="editTit">第 1 章：<span>本章的标题</span></div>
				                                <div class="editBtn tar">
				                                    <a href="javascript:;" class="save" vid="" v_name="" describe="" chapter_id="" ><span class="ebj">✎</span>编辑</a>
				                                	<a href="javascript:;" class="del" onclick="jsRemove(this,'dl');"><span>&times;</span>删除</a>
				                                </div>
				                            </dt>

											<!-- <dd>
												<div class="editTit"><span>填写讲的内容</span></div>
												<div class="editBtn tar">
													<a href="javascript:;" class="save" onclick="jsEdit(this, true);"><span class="ebj">✎</span>编辑</a>
													<a href="javascript:;" class="del" onclick="jsRemove(this,'dd');"><span>&times;</span>删除</a>
												</div>
											</dd> -->
											<dd class="bookAdd">
												<div class="editTit"><a href="javascript:;" onclick="jsBook_sub(this);"><span>+</span>添加一讲</a></div>
											</dd>
										</dl>
										<{/if}>
										<dl class="bookAdd">
											<dt><a href="javascript:;" onclick="jsBook_par(this);"><span>+</span>添加一章</a></dt>
										</dl>
									</div>
								</div>
								<!--end-->
							</div>
							<!--end 编辑课程目录-->
						</div>


					</div>
				</div>

				<!--end内容区域-->

				<script type="text/javascript">
					var inputHtml = '<div class="editText"><input type="text" class="text chapter" ></div>';
					//var confirmHtml = '<div class="confirmBtn"><a class="btn-confirm u2" href="javascript:;" onclick=jsConfirm(this);>确认</a><a class="btn-close u2" href="javascript:;" onclick=jsClose(this);>取消</a></div>';
					var confirmHtml = '<div class="confirmBtn"><a class="btn-confirm u2" is_pid="false" href="javascript:;">确认</a><a class="btn-close u2" href="javascript:;">取消</a></div>';
					//var ebj_div = '<a href="javascript:;" class="save"  describe="" chapter_id="0" onclick=jsEdit(this)><span class="ebj">✎</span>编辑</a>';
					var ebj_div = '<a href="javascript:;" class="save"  describe="" chapter_id="0" ><span class="ebj">✎</span>编辑</a>';
					var tit_div = '<div class="editTit"><span>填写讲的内容</span></div>';

					var btn_div = '<div class="editBtn tar">' + ebj_div + '<a href="javascript:;" class="del" onclick=jsRemove(this,"dd")><span>&times;</span>删除</a></div>';
					var subHtml = '';
					subHtml += '<dd>';
					subHtml += '' + tit_div + '';
					subHtml += '' + tit_div + '';
					subHtml += '</dd>';

					var parHtml = '';
					parHtml += '<dl>';
					parHtml += '<dt>';
					parHtml += '<div class="editTit"><span>填写章的内容</span></div>';
					parHtml += '<div class="editBtn tar">' + ebj_div + '<a href="javascript:;" class="del" onclick=jsRemove(this,"dl")><span>&times;</span>删除</a></div>';
					parHtml += '</dt>';
					parHtml += '<dd class="bookAdd">';
					parHtml += '<div class="editTit"><a href="javascript:;" onclick="jsBook_sub(this);"><span>+</span>添加一讲</a></div>';
					parHtml += '</dd>';
					parHtml += '</dl>';

					var subeditHtml = '';
					subeditHtml += '' + inputHtml + '';
					subeditHtml += '<div class="editInfo">';
					subeditHtml += '<div class="e_Textarea"><textarea class="describe"></textarea></div>';
					subeditHtml += '<div class="add_video"><a href="javascript:;" onclick="sel_video(this)">+选择视频</a></div>';
					subeditHtml += '<div class="sel_video"></div>';
					subeditHtml += '<div class="fix"><ul class="upload_A">';
					subeditHtml += '<li>';
					// subeditHtml += '<input id="myfile" style="display:none" type="file">';
					// subeditHtml += '<a href=javascript:document.getElementById("myfile").click()></a>';
					subeditHtml += '<div class="list"><div class="ith_img2 usernane_img120"><span></span></div>';
					subeditHtml += '</div>';
					subeditHtml += '</li>';
					subeditHtml += '</ul></div>';
					subeditHtml += '<div><label><input type="checkbox">允许免费试听，学生无需购买就可以免费学习整节课程。</label></div>';
					subeditHtml += '</div>';
					// subeditHtml += ''+confirmHtml+'';
					//subeditHtml += '<div class="confirmBtn"><a class="btn-confirm u2" href="javascript:;" onclick="jsConfirm(this, true)">确认</a><a class="btn-close u2" href="javascript:;" onclick=jsClose(this);>取消</a></div>';
					subeditHtml += '<div class="confirmBtn"><a class="btn-confirm u2" is_pid="true" href="javascript:;">确认</a><a class="btn-close u2" href="javascript:;" >取消</a></div>';

					var b_add = '.bookAdd';
					var curr = 'curr';

					var edt = '.editTit';
					var edb = '.editBtn';
					var e_Txt = '.editText';
					var c_Btn = '.confirmBtn';
					var sub_edt = '.subeditText';

					//添加一章
					function jsBook_par(objthis) {
						var $this = $(objthis);
						$this.parents(b_add).before(parHtml);
					}
					//添加一讲
					function jsBook_sub(objthis) {
						var $this = $(objthis);
						$this.parents(b_add).before('<dd class="subeditText"></dd>');
						$this.parents(b_add).prev(sub_edt).append(subeditHtml);
					}
					//删除按钮
					function jsRemove(objthis, objval) {
						var $this = $(objthis);
						$this.parents(objval).remove();
						var chapter_id = $this.attr("chapter_id");
						$.ajax({
							type: "POST",
							url: "/course/del_video_chapter",
							data: "id=" + chapter_id,
							async: false,
							dataType: "json",
							success: function(data) {

							}
						});
					}

					function sel_video(obj) {
						var index = $(obj).parent().index(".add_video");
						layer.open({
							type: 2,
							area: ['700px', '530px'],
							fixed: false, //不固定
							maxmin: true,
							content: '/course/select_video/0/0/0/0/0/' + index
						});
					}
					;$(function(){
					//编辑按钮
					//function jsEdit(objthis) {
					$(document).on("click", ".save", function () {
						//var $this = $(objthis);
						var $this = $(this);
						var t_val = $this.parent(edb).siblings(edt).text();
						
						$this.parent(edb).hide();
						$this.parent(edb).siblings().hide();
						if ($('dt')) { //章
							$this.parent(edb).parent('dt').addClass(curr).append(inputHtml, confirmHtml);
						};
						if ($('dd')) { //讲
							$this.parent(edb).parent('dd').addClass('subeditText').append(subeditHtml);
						};
						$this.parent(edb).siblings(e_Txt).children().val(t_val).focus();
						
						//if ($this.attr('is_pid');.length > 0) {
							var t_describe_val = $this.attr('describe');
							//alert($this.parent(edb).siblings('.editInfo').length);
							$this.parent(edb).siblings('.editInfo').children().find(".describe").eq(0).val(t_describe_val);
						//}
						
						var t_vid_val = $this.attr('vid');
						var t_v_name_val = $this.attr('v_name');
						$this.parent(edb).siblings('.editInfo').find(".sel_video").eq(0).html('<p id="vid_'+t_vid_val+'">'+t_v_name_val+'</p><input class="selected" name="vid" value="'+t_vid_val+'" type="hidden">');
					//}
					});
					//确认按钮
					//function jsConfirm(objthis, is_pid) {
					
					$(document).on("click", ".btn-confirm", function () {
						var is_pid = $(this).attr('is_pid');
						var objthis = this;
						//layer.alert($(".chapter").val())
						//
						if($(".chapter").val() == ""){
							layer.alert("请填写章节的内容!")
						}
						var $this = $(objthis);
						var course_id = <{$course_id}>;
						var f_val = $this.parent(c_Btn).siblings(e_Txt).children().val();
						if (f_val != 0 && f_val != null) {
							//var chapter_val = $(".chapter").val();
							var chapter_val = f_val;
							var chapter_id = $this.closest("dl").find(".save").eq(0).attr("chapter_id");
							if (is_pid == 'true') {
								if ($this.parent(c_Btn).siblings('.editInfo').children().find(".selected").eq(0).length > 0) {
									var f_course_vid = $this.parent(c_Btn).siblings('.editInfo').children().find(".selected").eq(0).val();
									//var course_vid = $(".selected").val();
									var course_vid = f_course_vid;
								}else{
									var course_vid = 0;
								}
								//alert(course_vid);
								if ($this.parent(c_Btn).siblings('.editInfo').children().find(".describe").eq(0).length > 0) {
									var f_describe = $this.parent(c_Btn).siblings('.editInfo').children().find(".describe").eq(0).val();
									//var describe = $(".describe").val();
									var describe =f_describe;
								}else{
									var describe = '';
								}
								//$this.siblings('.editDescribe').text(describe);
								//alert(describe);
								
								if (chapter_id == 0) {
									layer.alert("请填写章节的内容!");
									return false;
								}
								//alert($this.closest("dd").find(".save").eq(0).attr("chapter_id"));
								if ($this.closest("dd").find(".save").eq(0).length > 0) {
									var id = $this.closest("dd").find(".save").eq(0).attr("chapter_id");
								}else{
									var id = 0;
								}
								var data = "id=" + id + "&pid=" + chapter_id + "&name=" + chapter_val + "&course_id=" + course_id + "&vid=" + course_vid + "&describe=" + describe;
							} else {
								if(course_id == 0){
									layer.alert("请先添加课程!");
									return false;
								}
								var data = "id=" + chapter_id + "&name=" + chapter_val + "&course_id=" + course_id;
							}
							var del_id = 0;
							$.ajax({
								type: "POST",
								url: "/course/add_video_chapter",
								data: data,
								async: false,
								dataType: "json",
								success: function(data) {
									//alert(is_pid);
									//$("#chapter").attr("chapter_id", data);
									
									if (data.pid > 0) {
										var reutn_data_id = data.pid;
									} else {
										var reutn_data_id = data.id;
									}
									if (is_pid == 'true') {
										$this.closest("dd").find(".save").eq(0).attr("chapter_id", data.id);
										$this.closest("dd").find(".save").eq(0).attr("describe",describe);
										$this.closest("dd").find(".save").eq(0).attr("vid",course_vid);
										$this.closest("dd").find(".save").eq(0).attr("v_name",$('#vid_'+course_vid).html());
									}else{
										$this.closest("dl").find(".save").eq(0).attr("chapter_id", reutn_data_id);
									}
									//alert(reutn_data_id);
									//alert($this.closest("dl").find(".save").eq(0));
									del_id = data.id;
									
									// alert(data.id)
									if (is_pid != 'true') {
										$this.closest("dl").find(".save").eq(0).next().attr("chapter_id", data.id);
									} else {
										var del_obj = $this.closest("dl").find(".del").last();
										if (del_obj.attr("chapter_id") <= 0) {
											del_obj.attr("chapter_id", data.id);
										}
									}
								}
							});
							if ($this.parent(c_Btn).parent('dt').is(curr)) {
								$this.parent(c_Btn).parent('dt').removeAttr('class');
								$this.parent(c_Btn).siblings(edt).text(f_val).show();
								$this.parent(c_Btn).siblings(edb).show();
								$this.parent(c_Btn).siblings(e_Txt).remove();
								$this.parent(c_Btn).remove();
							} else {
								if ($this.parent(c_Btn).siblings().is(edt)) {


									$this.parent(c_Btn).siblings(edt).text(f_val).show();
									$this.parent(c_Btn).siblings(edb).show();
								} else {
									var btn_div = '<div class="editBtn tar">' + ebj_div + '<a href="javascript:;" class="del" chapter_id="' + del_id + '" onclick=jsRemove(this,"dd")><span>&times;</span>删除</a></div>';
									$this.parent(c_Btn).siblings(e_Txt).before(tit_div, btn_div);
									$this.parent(c_Btn).siblings(edt).text(f_val);
									$this.parent(c_Btn).siblings(edb);
								};
								$this.parent(c_Btn).parent(sub_edt).removeAttr('class');
								$this.parent(c_Btn).siblings(e_Txt).remove();
								$this.parent(c_Btn).siblings('.editInfo').remove();
								$this.parent(c_Btn).remove();
							};

						} else {
							$this.parent(c_Btn).siblings(e_Txt).children().focus();
							layer.alert("请填写章节的标题!");

						};
					});
					
					//}
					//取消按钮btn-close
					//function jsClose(objthis, objval) {
					$(document).on("click", ".btn-close", function () {
						///var $this = $(objthis);
						var $this = $(this);
						if ($this.parent(c_Btn).parent('dd').is(sub_edt)) {
							if (!$this.parent(c_Btn).siblings().is(edt)) { //这是添加讲（没有文本框的）
								$this.parent(c_Btn).parent(sub_edt).remove();
							} else { //这是编辑讲
								$this.parent(c_Btn).parent('dd').removeAttr('class');
								$this.parent(c_Btn).siblings().show();
								$this.parent(c_Btn).siblings(e_Txt).remove();
								$this.parent(c_Btn).parent('dd').find('.editInfo').remove();
								$this.parent(c_Btn).remove();
							}
						} else {
							$this.parent(c_Btn).parent('dt').removeAttr('class');
							$this.parent(c_Btn).siblings().show();
							$this.parent(c_Btn).siblings(e_Txt).remove();
							$this.parent(c_Btn).remove();
						};
					//}
					});
					});
					//讲-确认按钮
					function jsSubConfirm(objthis) {
						var $this = $(objthis);
						var f_val = $this.parent(c_Btn).siblings(e_Txt).children().val();
						if (f_val != 0 && f_val != null) {
							if ($this.parent(c_Btn).siblings().is(edt)) {
								$this.parent(c_Btn).siblings(edt).text(f_val).show();
								$this.parent(c_Btn).siblings(edb).show();
								$this.parent(c_Btn).parent(sub_edt).removeAttr('class');
								$this.parent(c_Btn).siblings(e_Txt).remove();
								$this.parent(c_Btn).remove();
							} else {
								$this.parent(c_Btn).siblings(e_Txt).before(tit_div, btn_div);
								$this.parent(c_Btn).siblings(edt).text(f_val);
								$this.parent(c_Btn).siblings(edb);
								$this.parent(c_Btn).parent(sub_edt).removeAttr('class');
								$this.parent(c_Btn).siblings(e_Txt).remove();
								$this.parent(c_Btn).remove();
							};
						} else {
							layer.alert("请填写章节的标题!");
							$this.parent(c_Btn).siblings(e_Txt).children().focus();
						};
					}

				</script>
</body>

</html>
